<?php
/**
 * Copyright (c) 2008-2011 Submit Consulting. Lima-Peru. All Right Reserved.
 * @author Ing. Angel Sullon M. (asullom@hotmail.com)
 * Created: 21.SEP.2000
 * Last Modify: 11.AGO.2011
 * Description of DiaData: CRUD
 */
class DiaData {
	//	DiaId,
	//	IteracionId,
	//	Fecha
	public function create(Dia $nuevo) {
		$tran= new ConnectDB();
		$tran->getConnection();
		$r=0;
		try{
			$tran->begin();
			$qry=" SELECT COALESCE(MAX(DiaId)+1,1) AS Id FROM Dia ";
			$rs=mysql_query($qry) or die("<p class=error>".mysql_error()." table Dia</p>");
			if( $row=mysql_fetch_array($rs) )
			$nuevo->setDiaId( $row["Id"] );
			$qry=" INSERT INTO Dia
            	(DiaId,IteracionId,Fecha)
                values(?,?,str_to_date(?,'%d/%c/%Y')) ";
			$pre = new PrepareQuery($qry);
			$pre->set($nuevo->getDiaId());
			$pre->set($nuevo->getIteracionRef()->getIteracionId());
			$pre->set($nuevo->getFecha());
			mysql_query($pre->getQuery()) or trigger_error("<p class=error>".mysql_error()." table Dia", E_USER_ERROR. "</p>");;
			$r=mysql_affected_rows();
			$tran->commit();
		}catch(Exception $e){
			$tran->rollback();
			throw new GlobalException("DiaData.create ".$e->getMessage());
		}
		$tran->close();
		return $r;
	}

	public function listByFilter($filter) {//CRUD create, reader, update, delete
		$tran= new ConnectDb();
		$tran->getConnection();
		$li=array();
		try{
			$qry=" SELECT u.DiaId,u.IteracionId,DATE_FORMAT(u.Fecha, '%d/%c/%Y') as Fecha
                FROM Dia u
                WHERE  UPPER(u.Fecha) LIKE UPPER(?)
                order by u.Fecha";
			$pre = new PrepareQuery($qry);
			$pre->set($filter."%");
			$rs=mysql_query($pre->getQuery()) or trigger_error("<p class=error>".mysql_error()." table Dia", E_USER_ERROR. "</p>");
			$u=0;
			while( $row=mysql_fetch_array($rs) ) {
				$d=new Dia();
				$d->setDiaId($row["DiaId"]);
				$d->getIteracionRef()->setIteracionId($row["IteracionId"]);
				$d->setFecha($row["Fecha"]);
				$li[$u++]=$d;
			}
			mysql_free_result($rs);
		}catch(Exception $e){
			throw new GlobalException("DiaData.listByFilter ".$e->getMessage());
		}
		$tran->close();
		return $li;
	}

	public function findByPk($id) {
		$tran= new ConnectDB();
		$tran->getConnection();
		$d=new Dia();
		try{
			$qry=" SELECT u.DiaId,u.IteracionId,DATE_FORMAT(u.Fecha, '%d/%c/%Y') as Fecha
                FROM Dia u
                WHERE  u.DiaId=?";
			$pre = new PrepareQuery($qry);
			$pre->set($id);
			$rs=mysql_query($pre->getQuery()) or trigger_error("<p class=error>".mysql_error()." table Dia", E_USER_ERROR. "</p>");;
			if( $row=mysql_fetch_array($rs) ) {
				$d->setDiaId($row["DiaId"]);
				$d->getIteracionRef()->setIteracionId($row["IteracionId"]);
				$d->setFecha($row["Fecha"]);
			}
			mysql_free_result($rs);
		}catch(Exception $e){
			throw new GlobalException("DiaData.findByPk ".$e->getMessage());
		}
		$tran->close();
		return $d;
	}

	public function update(Dia $dato) {
		$tran= new ConnectDB();
		$tran->getConnection();
		$r=0;
		try{
			$qry=" UPDATE Dia SET
                IteracionId = ?,Fecha = str_to_date(?,'%d/%c/%Y')
                WHERE DiaId=?";
			$pre = new PrepareQuery($qry);
			$pre->set($dato->getIteracionRef()->getIteracionId());
			$pre->set($dato->getFecha());
			$pre->set($dato->getDiaId());
			mysql_query($pre->getQuery()) or trigger_error("<p class=error>".mysql_error()." table Dia", E_USER_ERROR. "</p>");
			$r=mysql_affected_rows();
		}catch(Exception $e){
			throw new GlobalException("DiaData.update ".$e->getMessage());
		}
		$tran->close();
		return $r;
	}

	public function delete($id) {
		$tran= new ConnectDB();
		$tran->getConnection();
		$r=0;
		try{
			$qry=" DELETE FROM Dia WHERE DiaId=? ";
			$pre = new PrepareQuery($qry);
			$pre->set($id);
			mysql_query($pre->getQuery()) or trigger_error("<p class=error>".mysql_error()." table Dia", E_USER_ERROR. "</p>");;
			$r=mysql_affected_rows();
		}catch(Exception $e){
			throw new GlobalException("DiaData.delete ".$e->getMessage());
		}
		$tran->close();
		return $r;
	}

}
?>